<template>
 <div class="stockDeviceType">
   <div class="card-table">
    <el-button style="margin-bottom:12px;" icon="el-icon-plus" type="primary" @click="dialogVisible1 = true">新增</el-button>
    <el-table
      :header-cell-style="getRowClass"
      :data="tableData"
      style="width: 100%">
      <el-table-column
        align="center"
        type="index"
        label="序号"
        width="60">
      </el-table-column>
      <el-table-column
        align="center"
        prop="name"
        label="设备名"
        width="width">
      </el-table-column>
      <!-- <el-table-column
        align="center"
        label="规格型号"
        width="width">
        <template slot-scope="{row}">
          <el-tag size="medium" type="" v-for="item in row.specification" :key="item._id">{{item.name}}</el-tag>
        </template>
      </el-table-column> -->
      <el-table-column
        align="center"
        label="操作">
        <template slot-scope="{row}">
          <el-button @click="editForm(row);dialogVisible2=true" type="text">修改</el-button>
          <el-button style="margin-left:0;" @click="deleteOne(row)" type="text">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :modal-append-to-body="false"
      title="创建设备类型"
      :visible.sync="dialogVisible1"
      width="30%">
      <el-form  :rules="rules1" size="small" ref="formCreate" :model="formCreate" label-width="80px">
        <el-form-item label="设备名" prop="name">
          <el-input v-model="formCreate.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formCreate')">取 消</el-button>
        <el-button type="primary" @click="created()">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      :modal-append-to-body="false"
      title="修改设备类型"
      :visible.sync="dialogVisible2"
      width="30%">
      <el-form :rules="rules2" size="small" ref="formEdit" :model="formEdit" label-width="80px">
        <el-form-item label="设备名" prop="name">
          <el-input v-model="formEdit.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formEdit')">取 消</el-button>
        <el-button type="primary" @click="edit()">确 定</el-button>
      </span>
    </el-dialog>
   </div>
 </div>
</template>

<script>
import { stockDeviceTypeGet, stockDeviceTypeCreate, stockDeviceTypeUpdate, stockDeviceTypeDelete, specificationGet } from '@/api/spare.js'

export default {
  name: 'stockDeviceType',
  components: {},
  data () {
    return {
      dialogVisible1: false,
      dialogVisible2: false,
      specifications: [],
      formCreate: {
      },
      formEdit: {
        name: ''
      },
      tableData: [],
      rules1: {
        name: [
          { required: true, message: '请输入设备名', trigger: 'blur' }
        ]
      },
      rules2: {
        name: [
          { required: true, message: '设备名不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {
    this.getData()
    specificationGet().then(res => {
      this.specifications = res.data.data.pageData
    })
  },
  methods: {
    getData () {
      stockDeviceTypeGet().then(res => {
        console.log(res.data)
        this.tableData = res.data.data.pageData
      })
    },
    deleteOne (row) {
      console.log(row)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        stockDeviceTypeDelete(row._id).then(res => {
          console.log(res)
          if (res.data.code === 200) {
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.getData()
          } else {
            this.$message({
              type: 'warning',
              message: res.data.message
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    created () {
      this.$refs.formCreate.validate((valid) => {
        if (valid) {
          stockDeviceTypeCreate(this.formCreate).then(res => {
            if (res.data.code === 200) {
              this.$message({
                type: 'success',
                message: '创建成功'
              })
              this.getData()
              this.dialogVisible1 = false
              this.$refs.formCreate.resetFields()
            } else {
              this.$message({
                type: 'warning',
                message: res.data.message
              })
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    editForm (row) {
      this.formEdit._id = row._id
      this.formEdit.name = row.name
    },
    edit () {
      this.$refs.formEdit.validate((valid) => {
        if (valid) {
          console.log(this.formEdit)
          stockDeviceTypeUpdate(this.formEdit).then(res => {
            console.log(res)
            if (res.data.code === 200) {
              this.$message({
                type: 'success',
                message: '修改成功'
              })
              this.getData()
              this.dialogVisible2 = false
              this.$refs.formEdit.resetFields()
            } else {
              this.$message({
                type: 'warning',
                message: res.data.message
              })
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm (formName) {
      this.dialogVisible1 = false
      this.dialogVisible2 = false
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style  scoped>
.el-tag ~ .el-tag{
  margin-left: 4px;
}
</style>
